package com.yunhe.dao.impl;


import java.util.List;


import org.hibernate.Query;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yunhe.dao.IItemDao;
import com.yunhe.entity.Item;

/**
 * 投票取值Dao层实现
 * */
public class ItemDaoImpl extends HibernateDaoSupport implements IItemDao {
	/*private SessionFactory sf = null;
	
	public SessionFactory getSf() {
		return sf;
	}
	public void setSf(SessionFactory sf) {
		this.sf = sf;
	}*/
	/**
	 * 条件查询集合
	 * */
	@Override	
	public List<Item> searchItems(Item item) {
		//书写hql语句
		String hql = "from Item where 1 = 1";
		if(item.getOption()!=null){
			hql+=" and option =:option";
		}
		if(item.getSubject()!=null){
			hql+=" and subject =:subject";
		}
		if(item.getUser()!=null){
			hql+=" and user =:user";
		}
		Query query = this.getSession().createQuery(hql);
		query.setProperties(item);
		List<Item> list = query.list();
		if(list.size()==0){
			return null;
		}
		return list;
		//创建list返回对象
		/*List<Item> list = null;
		Session session = null;
		try {
			//创建session对象
			//Session session = HibernateUtil.currentSession();
			session = this.getSession();
			//书写hql语句
			String hql = "from Item where 1 = 1";
			if(item.getOption()!=null){
				hql+=" and option =:option";
			}
			if(item.getSubject()!=null){
				hql+=" and subject =:subject";
			}
			if(item.getUser()!=null){
				hql+=" and user =:user";
			}
			//生成query结果
			Query query = session.createQuery(hql);
			
			//hql语句赋值
			query.setProperties(item);
			//返回list结果
			list = query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			//关闭session
			//HibernateUtil.closeSession();
			if(session!=null&&session.isOpen()){
				session.close();
			}
		}
		return list;*/
	}
	/**
	 * 条件查询对象
	 * */
	@Override
	public Item searchItem(Integer id) {
		String hql = "from Item where vi_id = "+id;
		List<Item> list = (List<Item>) this.getHibernateTemplate().find(hql);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
		//创建返回对象
		/*Item item = null;
		Session session = null;
		try {
			//创建session对象
			//Session session = HibernateUtil.currentSession();
			session = sf.openSession();
			//书写hql语句
			String hql = "from Item where vi_id = "+id;
			
			//生成query结果
			Query query = session.createQuery(hql);
			
			//返回结果
			item = (Item)query.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			//关闭session
			//HibernateUtil.closeSession();
			if(session!=null&&session.isOpen()){
				session.close();
			}
		}
		return item;*/
	}
	/**
	 * 增加
	 * */
	@Override
	public void addItem(Item item) {
		this.getHibernateTemplate().save(item);
		/*Session session = null;
		try {
			//创建session对象
			//session = HibernateUtil.currentSession();
			session = sf.openSession();
			//开启transaction
			session.beginTransaction();
			//执行添加方法
			session.save(item);
			//提交
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			e.printStackTrace();
			//错误回滚
			session.getTransaction().rollback();
		}finally{
			//关闭session
			//HibernateUtil.closeSession();
			if(session!=null&&session.isOpen()){
				session.close();
			}
		}
		*/
	}
	/**
	 * 删除
	 * */
	@Override
	public void delItem(Item item) {
		this.getHibernateTemplate().delete(item);
		/*Session session = null;
		try {
			//创建session对象
			//session = HibernateUtil.currentSession();
			session = sf.openSession();
			//开启transaction
			session.beginTransaction();
			//执行方法
			session.delete(item);
			//提交
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			e.printStackTrace();
			//错误回滚
			session.getTransaction().rollback();
		}finally{
			//关闭session
			//HibernateUtil.closeSession();
			if(session!=null&&session.isOpen()){
				session.close();
			}
		}
		*/
	}
	/**
	 * 修改
	 * */
	@Override
	public void updateItem(Item item) {
		this.getHibernateTemplate().update(item);
		/*Session session = null;
		try {
			//创建session对象
			//session = HibernateUtil.currentSession();
			session = sf.openSession();
			//开启transaction
			session.beginTransaction();
			//执行方法
			session.update(item);
			//提交
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			e.printStackTrace();
			//错误回滚
			session.getTransaction().rollback();
		}finally{
			//关闭session
			//HibernateUtil.closeSession();
			if(session!=null&&session.isOpen()){
				session.close();
			}
		}
		*/
	}
	@Override
	public Integer searchItemCount() {
		String hql = "select count(*) from Item";
		Number n = (Number) this.getSession(true).createQuery(hql).uniqueResult();
		Integer count = n.intValue();
		return count;
	}
		/*Integer count = null;
		Session session  = null;
		try {
			//创建session对象
			//Session session = HibernateUtil.currentSession();
			session = sf.openSession();
			//书写hql语句
			String hql = "select count(*) from Item";
		
			//生成query结果
			Query query = session.createQuery(hql);
			
			Number num = (Number)query.uniqueResult();
			count = num.intValue();
			
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			//关闭session
			//HibernateUtil.closeSession();
			if(session!=null&&session.isOpen()){
				session.close();
			}
		}
		return count;
	}*/

}
